package com.lh.principle.composite;

import java.util.ArrayList;
import java.util.List;

public class Demo2 {
	public static void main(String[] args) {
		School school = new School(); // 创建一个学校对象

		// 创建学生对象
		Student student1 = new Student("Alice");
		Student student2 = new Student("Bob");
		Student student3 = new Student("Charlie");

		// 将学生对象添加到学校中
		school.addStudent(student1);
		school.addStudent(student2);
		school.addStudent(student3);

		// 显示学校中的学生
		school.displayStudents();
	}
}

//学生类
class Student {
	private String name;

	public Student(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}
}

//学校类
class School {
	private List<Student> students; // 使用聚合关系，将学生聚合到学校中

	public School() {
		students = new ArrayList<>(); // 在构造函数中创建学生列表对象
	}

	public void addStudent(Student student) {
		students.add(student); // 将学生对象添加到学生列表中
	}

	public void displayStudents() {
		for (Student student : students) {
			System.out.println(student.getName());
		}
	}
}
